package com.igoal.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import com.igoal.comparator.GoalEntryComparator;
import com.igoal.dto.UserGoalEntryDTO;
import com.igoal.dto.UserGoalEntryDoneDTO;
import com.igoal.entity.DataStatus;
import com.igoal.service.GoalEntryManager;
import com.igoal.service.GoalManager;
import com.igoal.service.MessageManager;
import com.igoal.service.UserManagerExt;
import com.igoal.service.paging.PageItem;
import com.igoal.service.paging.PagingService;
import com.igoal.utils.ConvertUtils;
import com.igoal.web.model.UserDashboardModel;

@Controller
@RequestMapping("/ajax/goalEntryView.html")
public class AjaxGoalEntryViewController extends BaseAjaxController {
	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}
	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}
	@Autowired
	public void setMessageManager(MessageManager messageManager){
		this.messageManager = messageManager;
	}
	
	@Autowired
	public void setGoalEntryManager(GoalEntryManager goalEnryManager) {
		this.goalEntryManager = goalEnryManager;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String show(ModelMap modelMap, @RequestParam("userId") String userId, @RequestParam("goalId") String goalId, 
			@RequestParam("lastOrderValue") String lastOrderValue){
		if(isLoginUser()){
			getUserSession();
		}
		
		UserDashboardModel model = new UserDashboardModel();
		
		Map<String, Object> keyValues = new HashMap<String, Object>();
		if(!StringUtils.isBlank(userId)){
			Long uid = ConvertUtils.toLong(userId, -1L);
			if(uid > 0L){
				keyValues.put("userGoal.pk.user.id", uid);
			}
		}
		
		if(!StringUtils.isBlank(goalId)){
			Long gid = ConvertUtils.toLong(goalId, -1L);
			if(gid > 0L){
				keyValues.put("userGoal.pk.goal.id", gid);
			}
		}
		
		keyValues.put("status", DataStatus.USING);
		
		Date date = null;
		if(!StringUtils.isBlank(lastOrderValue)){
			Long time = ConvertUtils.toLong(lastOrderValue, -1L);
			if(time > 0L){
				date = new Date(time);
			}
		}
		
		String orderProperty = PagingService.SORT_ENTRY_BY_PROPERTY;
		
		List<UserGoalEntryDTO> entries = goalEntryManager.getEntriesByPaging(keyValues, orderProperty, date, false);
		List<UserGoalEntryDoneDTO> doneEntries = goalEntryManager.getDoneEntriesByPaging(keyValues, orderProperty, date, false);
		
		List<PageItem<Long>> recentEntries = new ArrayList<PageItem<Long>>(); 
		if(doneEntries != null){
			recentEntries.addAll(doneEntries);
		}
		if(entries != null){
			recentEntries.addAll(entries);
		}
		
		Collections.sort(recentEntries, new GoalEntryComparator(false));
		
		List<PageItem<Long>> result = new ArrayList<PageItem<Long>>();
		
		for(int index=0; index<PagingService.MAX_ENTRIES && index<recentEntries.size(); index++){
			result.add(recentEntries.get(index));
		}
		
		// if(user != null){
		// model.setUserId(user.getId());
		// }

		model.setRecentEntries(result);
		
		initSessionInformation(model);
		
		applyModel(modelMap, model);		
		
		return getViewPrefix() + "ajaxGoalEntryView";
	}
}
